package com.panda.web.commons.handler;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.panda.common.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component//要丢进springboot中
@Slf4j//日志功能
public class MyMetaObjectHandler implements MetaObjectHandler {
    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert.... ");
        System.out.println("添加呈送");
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 将 Date 转换为指定格式的 String
        String dateString = formatter.format(date);

        //设置要往哪个数据库的列表添加数据
        this.setFieldValByName("createTime",date,metaObject);
        this.setFieldValByName("updateTime",date,metaObject);
        this.setFieldValByName("creationTime",dateString,metaObject);
        this.setFieldValByName("turnoverTime",dateString,metaObject);
    }

    //更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update.... ");
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 将 Date 转换为指定格式的 String
        String dateString = formatter.format(date);
        this.setFieldValByName("updateTime",date,metaObject);
        this.setFieldValByName("turnoverTime",dateString,metaObject);

    }
//    //删除时的填充策略
//    @Override
//    public void deletedFill(MetaObject metaObject) {
//        this.setFieldValByName("isDeleted","1",metaObject);
//
//    }
}

